package com.freecloud.javabasics.thread.semaphore;

import java.util.concurrent.Semaphore;

/**
 * 场景：限流(AQS)
 * @Author: maomao
 * @Date: 2020-02-23 17:51
 */
public class SemaphoreDemo {


    //permites; 令牌(5)
    //只有5个停车位

    //公平和非公平


    static class Car extends Thread{
        private int num;
        private Semaphore semaphore;


        public Car(int num,Semaphore semaphore){
            this.num = num;
            this.semaphore = semaphore;
        }

        @Override
        public void run() {
            try {
                semaphore.acquire();//获得一个令牌，如果拿不到令牌，就会阻塞
                System.out.println("第" + num + "抢到一个车位");

                Thread.sleep(2000);
                System.out.println("第" + num + "开走");
                semaphore.release();

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(5);
        for (int i = 0; i < 10; i++) {
            new Car(i,semaphore).start();
        }
    }


}
